import 'package:edu/pages/common/five_start_rating_widget.dart';
import 'package:flutter/material.dart';
import 'package:edu/constants/app_strings.dart';
import 'package:edu/pages/course/course_review_reply/course_review_reply_page_logic.dart';
import 'package:provider/provider.dart';

class CourseReviewReplyPage extends StatefulWidget {
  const CourseReviewReplyPage({super.key});

  @override
  State<CourseReviewReplyPage> createState() => _CourseReviewReplyPageState();
}

class _CourseReviewReplyPageState extends State<CourseReviewReplyPage> {
  @override
  void initState() {
    super.initState();
    context.read<CourseReviewReplyPageLogic>().init();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: _buildAppBar(context),
      body: _buildMainBody(context),
    );
  }

  AppBar _buildAppBar(BuildContext context) {
    final fontSize = Theme.of(context).textTheme.titleMedium?.fontSize ?? 16;
    return AppBar(
      title: Text(AppStrings.editReply, style: TextStyle(fontSize: fontSize)),
      leadingWidth: 80,
      leading: TextButton(
        child: Text(AppStrings.cancel, style: TextStyle(fontSize: fontSize)),
        onPressed: () {
          Navigator.of(context).pop();
        },
      ),
      actions: [
        TextButton(
          child: Text(AppStrings.confirm, style: TextStyle(fontSize: fontSize)),
          onPressed: () {
            final logic = context.read<CourseReviewReplyPageLogic>();
            logic.replyReview().then((value) {
              if (value != null) {
                Navigator.of(context).pop(value);
              }
            });
          },
        ),
      ],
    );
  }

  Widget _buildMainBody(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const SizedBox(height: 20),
          _buildUserReviewWidget(context),
          const SizedBox(height: 30),
          _buildReplyEditorWidget(context),
        ],
      ),
    );
  }

  Widget _buildReplyEditorWidget(BuildContext context) {
    final logic = context.read<CourseReviewReplyPageLogic>();
    return TextField(
      decoration: const InputDecoration(
        labelText: AppStrings.myReply,
        labelStyle: TextStyle(fontSize: 22, height: 0.75),
        hintText: AppStrings.replyHint,
        contentPadding: EdgeInsets.only(top: 0),
      ),
      controller: logic.controller,
      focusNode: logic.focusNode,
      autofocus: true,
      maxLength: 500,
      maxLines: 8,
    );
  }

  Widget _buildUserReviewWidget(BuildContext context) {
    final logic = context.read<CourseReviewReplyPageLogic>();
    final userName = logic.reviewUser;
    final reviewRating = logic.ratting;
    final review = logic.reviewContent;
    return ConstrainedBox(
      constraints: const BoxConstraints(maxHeight: 180),
      child: Container(
        padding: const EdgeInsets.all(12),
        decoration: BoxDecoration(
          color: Theme.of(context).colorScheme.surface,
          borderRadius: BorderRadius.circular(8),
        ),
        child: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                children: [
                  Expanded(
                    child: Text(
                      userName,
                      style: Theme.of(context).textTheme.titleMedium?.copyWith(
                        fontWeight: FontWeight.bold,
                      ),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),
                  ),
                  const SizedBox(width: 8),
                  FiveStartRatingWidget(rating: reviewRating, starSize: 16),
                ],
              ),
              const SizedBox(height: 10),
              Text(
                review,
                style: Theme.of(
                  context,
                ).textTheme.bodyMedium?.copyWith(height: 1.75),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
